package com.wxg.demo;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed.
 * -	* <https://stackoverflow.com/questions/20950748/cannot-find-firefox-binary-in-path-make-sure-firefox-is-installed>
 * -	* <https://github.com/mozilla/geckodriver/issues/480>
 * 
 * 
 * 
 * 
 * @author Wxg
 *
 */
public class SampleDemo101 {
	public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        
        code102_32();
        
    }
	
	/**
	 * <https://stackoverflow.com/questions/39875072/need-help-to-resolve-issue-in-webdrivercannot-find-firefox-binary-in-path-make>
	 * -	* Exception in thread "main" java.lang.IllegalStateException: 
	 * The path to the driver executable must be set by the webdriver.gecko.driver system property;
	 *  for more information, see https://github.com/mozilla/geckodriver. 
	 *  The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
	 * -	* 
	 */
	@Deprecated
	public static void code101(){
		// E:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe
		// C:\\Softwares\\Selenium\\Firefox driver\\geckodriver.exe
		// System.setProperty("webdriver.Firefoxdriver","E:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
		System.setProperty("webdriver.firefox.bin","E:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
		
		WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");
        WebElement element = driver.findElement(By.name("q"));
        element.sendKeys("Cheese!");
        element.submit();
        System.out.println("Page title is: " + driver.getTitle());
        
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });
        
        System.out.println("Page title is: " + driver.getTitle());
        driver.quit();
	}
	
	/**
	 * <https://stackoverflow.com/questions/20950748/cannot-find-firefox-binary-in-path-make-sure-firefox-is-installed/23910165#23910165>
	 * -	* Exception in thread "main" java.lang.IllegalStateException: 
	 * The path to the driver executable must be set by the webdriver.gecko.driver system property; 
	 * for more information, see https://github.com/mozilla/geckodriver. 
	 * The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
	 * 
	 */
	public static void code102(){
		// F:\\Downloads\\geckodriver-v0.19.1-win64
		// System.setProperty("webdriver.gecko.driver","D:\\Workspace\\demoproject\\src\\lib\\geckodriver.exe");
		System.setProperty("webdriver.gecko.driver","F:\\Downloads\\geckodriver-v0.19.1-win64\\geckodriver.exe");
		File pathBinary = new File("E:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
		FirefoxBinary firefoxBinary = new FirefoxBinary(pathBinary);   
		DesiredCapabilities desired = DesiredCapabilities.firefox();
		FirefoxOptions options = new FirefoxOptions();
		desired.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options.setBinary(firefoxBinary));
		WebDriver driver = new FirefoxDriver(options);
		
		
		
        driver.get("http://www.google.com");
        WebElement element = driver.findElement(By.name("q"));
        element.sendKeys("Cheese!");
        element.submit();
        System.out.println("Page title is: " + driver.getTitle());
        
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });
        
        System.out.println("Page title is: " + driver.getTitle());
        driver.quit();
	}
	
	public static void code102_32(){
		// F:\\Downloads\\geckodriver-v0.19.1-win64
		// System.setProperty("webdriver.gecko.driver","D:\\Workspace\\demoproject\\src\\lib\\geckodriver.exe");
		System.setProperty("webdriver.gecko.driver","F:\\Downloads\\geckodriver-v0.19.1-win32\\geckodriver.exe");
		File pathBinary = new File("E:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
		FirefoxBinary firefoxBinary = new FirefoxBinary(pathBinary);   
		DesiredCapabilities desired = DesiredCapabilities.firefox();
		FirefoxOptions options = new FirefoxOptions();
		desired.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options.setBinary(firefoxBinary));
		WebDriver driver = new FirefoxDriver(options);
		
		
		
        driver.get("http://www.google.com");
        WebElement element = driver.findElement(By.name("q"));
        element.sendKeys("Cheese!");
        element.submit();
        System.out.println("Page title is: " + driver.getTitle());
        
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("cheese!");
            }
        });
        
        System.out.println("Page title is: " + driver.getTitle());
        driver.quit();
	}
	
}
